package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BDeviceQuantitiesEntity;
import com.hongxun.modules.api.entity.BMaterialQuantitiesEntity;
import com.hongxun.modules.api.service.BDeviceQuantitiesService;
import com.hongxun.modules.api.service.BMaterialQuantitiesService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.APBaseService;
import com.hongxun.modules.bss.service.DMaterialCostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.service.DDeviceBuyService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 点位设备购置信息表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-04 10:42:47
 */
@RestController
@RequestMapping("/bss/ddevicebuy")
public class DDeviceBuyController {
	@Autowired
	private DDeviceBuyService dDeviceBuyService;
	@Autowired
	private DMaterialCostService dMaterialCostService;
	@Autowired
	private APBaseService aPBaseService;
	@Autowired
	private BDeviceQuantitiesService bDeviceQuantitiesService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";

		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
		}
		if(query.get("qName")!=null&&!query.get("qName").toString().equals(""))
		{
			wheres+=" and  d_b_name like  '%" + query.get("qName").toString()+"%' ";
		}
		if(query.get("qLabel")!=null&&!query.get("qLabel").toString().equals(""))
		{
			wheres+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
		}
		wheres+=" and  d_b_price>0 ";
		wheres+=" and  d_b_datastate != '删除' ";

        query.put("filterSql",wheres+" ");
		List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryList(query);
		int total = dDeviceBuyService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dDeviceBuyList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		DDeviceBuyEntity dDeviceBuy = dDeviceBuyService.queryObject(guid);
		
		return R.ok().put("dDeviceBuy", dDeviceBuy);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody DDeviceBuyEntity dDeviceBuy){

		//	2019年7月5日15:03:19	这段是大修技改项目要求，大荔和遵义项目打包时，请注释该段代码
		//	新增之前先查询点位设备表中是否有和需要添加数据中的设备id相同的数据，如有则将新增该条数据的单价改为表中已有同设备id数据的单价，再计算合价
		/*Map<String , Object> objectMap = new HashMap<>();
		String whereDeviceId = " 1=1 and d_d_guid='"+dDeviceBuy.getDDGuid()+"' ";
		objectMap.put("filterSql",whereDeviceId);
		List<DDeviceBuyEntity> dDeviceBuyEntities = dDeviceBuyService.queryList(objectMap);
		if(dDeviceBuyEntities != null){
			int ios = 0;

			for (DDeviceBuyEntity dDeviceBuyEntity : dDeviceBuyEntities) {
				if(ios == 0)
					dDeviceBuy.setDBPrice(dDeviceBuyEntity.getDBPrice());		//	单价
				++ios;
			}
			dDeviceBuy.setDBPriceall(dDeviceBuy.getDBPrice().multiply(BigDecimal.valueOf(dDeviceBuy.getDBCount())));	//	合价
			dDeviceBuyService.save(dDeviceBuy);	//	保存设备
		}else{
			dDeviceBuyService.save(dDeviceBuy);
		}*/


		//	以下是遵义和大荔项目，打包时需要放开注释的两行代码
		dDeviceBuyService.save(dDeviceBuy);
		APBaseEntity aPBaseEntity = aPBaseService.queryObject(dDeviceBuy.getPGuid());
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  d_guid  = '" + dDeviceBuy.getDDGuid() + "' ";
		wheres += " and  d_q_type =  " + aPBaseEntity.getPBType()  ;
		wheres += " and  d_q_work = 1 ";
		mapaPOther.put("filterSql", wheres + " ");
		List<BDeviceQuantitiesEntity> listBDeviceQuantitiesEntity=	bDeviceQuantitiesService.queryList(mapaPOther);
		for(BDeviceQuantitiesEntity bDeviceQuantitiesEntity :listBDeviceQuantitiesEntity)
		{
			DMaterialCostEntity dMaterialCostEntity = new DMaterialCostEntity();
			dMaterialCostEntity.setPPGuid(dDeviceBuy.getPPGuid());
			dMaterialCostEntity.setPGuid(dDeviceBuy.getPGuid());
			dMaterialCostEntity.setPMGuid(dDeviceBuy.getGuid());
			dMaterialCostEntity.setMMGuid(dDeviceBuy.getDDGuid());
			dMaterialCostEntity.setMCWork("1");
			dMaterialCostEntity.setMCQuantities(bDeviceQuantitiesEntity.getDQQuantities());
			dMaterialCostEntity.setMCExplain(bDeviceQuantitiesEntity.getDQExplain());
			dMaterialCostEntity.setMCUnit(bDeviceQuantitiesEntity.getDQUnit());
			dMaterialCostEntity.setMCMeterUnit(bDeviceQuantitiesEntity.getDQMeterUnit());
			dMaterialCostEntity.setMCCount(new BigDecimal(dDeviceBuy.getDBCount()) );
			dMaterialCostEntity.setMCPrice(bDeviceQuantitiesEntity.getDQPrice());
			dMaterialCostEntity.setMCPriceall(Priceall(dMaterialCostEntity.getMCCount(), dMaterialCostEntity.getMCPrice()));
			dMaterialCostEntity.setMCRatio(bDeviceQuantitiesEntity.getDQRatio());
			dMaterialCostEntity.setMCDatastate("新增");
			dMaterialCostEntity.setMMName(dDeviceBuy.getDBName());
			dMaterialCostEntity.setMMType(0);
			dMaterialCostEntity.setPMSource(2);
			dMaterialCostService.save(dMaterialCostEntity);
		}
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody DDeviceBuyEntity dDeviceBuy) {

		dDeviceBuy.setDBPriceall(Priceall(dDeviceBuy.getDBPrice(), new BigDecimal(dDeviceBuy.getDBCount())));
		dDeviceBuyService.update(dDeviceBuy);
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  p_m_guid = '" + dDeviceBuy.getGuid() + "' ";
		mapaPOther.put("filterSql", wheres + " ");
		List<DMaterialCostEntity> dMaterialCostList = dMaterialCostService.queryList(mapaPOther);
		for (DMaterialCostEntity dMaterialCostEntity :dMaterialCostList) {
			dMaterialCostEntity.setMCCount(new BigDecimal(dDeviceBuy.getDBCount()) );
			dMaterialCostEntity.setMCPriceall(Priceall(dMaterialCostEntity.getMCPrice(),dMaterialCostEntity.getMCCount()));
			dMaterialCostService.update(dMaterialCostEntity);
		}
		return R.ok();
	}

	/**
	 * 修改单价和数量
	 * @param Parms
	 * @return
	 */
	@RequestMapping("/updateCell")
	public R update(@RequestParam Map<String , Object> Parms){

		DDeviceBuyEntity dDeviceBuy = dDeviceBuyService.queryObject(Parms.get("guid").toString());
		BigDecimal Price = new BigDecimal(Parms.get("dBPrice").toString());
		dDeviceBuy.setDBPrice(Price);
		dDeviceBuy.setDBPriceall(Priceall(Price,new BigDecimal(dDeviceBuy.getDBCount())));
		dDeviceBuy.setDBDatastate("修改");
		dDeviceBuyService.update(dDeviceBuy);

		return R.ok();
	}
	@RequestMapping("/upPriceother")
	public R upPriceother( @RequestBody String   guid){
		DDeviceBuyEntity dDeviceBuy = dDeviceBuyService.queryObject(guid);


		Map<String ,Object> mapaPOther = new HashMap<>();
		String wheres=" 1=1 ";
		wheres+=" and  p_guid = '" + dDeviceBuy.getPGuid()+"' ";
		wheres+=" and  d_d_guid = '" + 	dDeviceBuy.getDDGuid()+"' ";
		mapaPOther.put("filterSql",wheres+" ");
		List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryList(mapaPOther);
		for(DDeviceBuyEntity dDeviceBuyEntity: dDeviceBuyList)
		{
			dDeviceBuyEntity.setDBPrice(dDeviceBuy.getDBPrice());

			dDeviceBuyEntity.setDBPriceall(Priceall(dDeviceBuy.getDBPrice(),new BigDecimal(dDeviceBuyEntity.getDBCount())));
			dDeviceBuyEntity.setDBDatastate("修改");
			dDeviceBuyService.update(dDeviceBuyEntity);
		}
		return R.ok();
	}
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		dDeviceBuyService.deleteBatch(guids);
		dMaterialCostService.deleteBatchByMBguid(guids);
		return R.ok();
	}


	/**
	 * 	大修技改手机端修改价格
	 * @param Parms
	 * @return
	 */
	@RequestMapping("/updateCellForPhone")
	public R updateCellForPhone(@RequestParam Map<String , Object> Parms){

		DDeviceBuyEntity dDeviceBuy = dDeviceBuyService.queryObject(Parms.get("guid").toString());
		BigDecimal Price = new BigDecimal(Parms.get("dBPrice").toString());
		dDeviceBuy.setDBPrice(Price);
		dDeviceBuy.setDBPriceall(Priceall(Price,new BigDecimal(dDeviceBuy.getDBCount())));
		dDeviceBuy.setDBDatastate("修改");
		dDeviceBuyService.update(dDeviceBuy);
		upPriceother(Parms.get("guid").toString());

		return R.ok();
	}


	/**
	 * 	购置表
	 * @param params
	 * @return
	 */
	@RequestMapping("/purchaseList")
	public R purchaseList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		String wheres2 =" 1=1 ";

		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
			wheres2+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
//		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
//		{
//			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
//		}
		if(query.get("qName")!=null&&!query.get("qName").toString().equals(""))
		{
			wheres+=" and  d_b_name like  '%" + query.get("qName").toString()+"%' ";
			wheres2+=" and  m_b_name like  '%" + query.get("qName").toString()+"%' ";
		}
		if(query.get("qLabel")!=null&&!query.get("qLabel").toString().equals(""))
		{
			wheres+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
			wheres2+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
		}
		wheres+=" and  d_b_price>0 ";
		wheres2+=" and  m_b_price>0 ";
		wheres+=" and  d_b_datastate != '删除' ";
		wheres2+=" and  m_b_datastate != '删除' ";

		query.put("filterSql",wheres+" ");
		query.put("filterSql2",wheres2+" ");
		List<PurchaseEntity> dDeviceBuyList = dDeviceBuyService.purchaseList(query);
		int total = dDeviceBuyService.queryTotalForPurchase(query);
//		int total = dDeviceBuyList.size();

		PageUtils pageUtil = new PageUtils(dDeviceBuyList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}










	private static BigDecimal Priceall(BigDecimal Price , BigDecimal Count){

		BigDecimal Priceall = null;
		Priceall = Price.multiply(Count);
		return Priceall;
	}
	
}
